var woody = {
    loadPage: function () {
        var page = this._parseParams()['page'];
        if (!page || page.length == 0) page = 'index';
        $.ajax({
            url: './template/' + page + '.html',
            context: this,
            success: function (result) {
                this._renderPage(page, result);
            }
        });
    },
    loadBlog: function (target) {
        $.ajax({
            url: './docs/' + target.text + '.md',
            context: this,
            success: function (result) {
                this._renderBlog(target, result)
            }
        });
    },
    _parseParams: function () {
        var result = {};
        var search = window.location.search;
        if (search && search.length > 1) {
            var params = search.substring(1);
            var items = params.split('&');
            for (var i = 0; i < items.length; ++i) {
                var item = items[i];
                var pos = item.search('=');
                if (pos > 0) {
                    var key = item.substring(0, pos);
                    result[key] = item.substring(pos + 1);
                }
            }
        }
        return result;
    },
    _renderPage: function (page, content) {
        var items = { 'index': 1, 'blog': 2, 'gallery': 3, 'about': 4 };
        var index = items.hasOwnProperty(page) ? items[page] : 0;
        $('#pages>li:nth-child(' + index + ')').addClass('active');
        $('footer').addClass('hidden');
        $('#content').html(content);
        $('footer').removeClass('hidden');
    },
    _markdown: window.markdownit({
        highlight: function (str, lang) {
            return hljs.highlight(lang, str).value;
        }
    }),
    _renderBlog: function (target, content) {
        $('#markdown').html(this._markdown.render(content));
        $('#filelist>li').removeClass('active');
        $(target).parent().addClass('active');
        window.scrollTo(0, 0);
    },
}